package com.suning.sawp.service.encrypt;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;
import com.suning.framework.security.asymmetric.impl.RSACoder;
import com.suning.framework.security.digest.impl.Base64Coder;

/**
 * 加密工具类
 * 
 * @author 14043094
 *
 */
public final class EncryptUtil {
	/**
	 * logger
	 */
	private static Logger LOGGER = LoggerFactory.getLogger(EncryptUtil.class);

	private static final String AES_ENCODING = "UTF-8";
	
	private EncryptUtil() {
	}
	
	private static MessageDigest digest;

	static {
		try {
			digest = MessageDigest.getInstance("SHA-1");
		} catch (NoSuchAlgorithmException e) {
			LOGGER.error("MessageDigest.getInstance('SHA-1') find wrong.", e);
		}
	}

	/**
	 * 字符串进行sha1加密
	 * 
	 * @param strSrc
	 *            加密前字符串
	 * @return 加密后的字符串
	 */
	public static final String encrypt(String strSrc) {
		String strDes = null;
		byte[] bt = strSrc.getBytes(Charsets.UTF_8);
		digest.update(bt);
		strDes = byte2hex(digest.digest());
		return strDes;
	}

	/**
	 * 将字节数组转换成16进制字符串
	 * 
	 * @param b
	 *            字符节数组
	 * @return 转换后的16进制数
	 */
	private static String byte2hex(byte[] b) {
		StringBuilder sbDes = new StringBuilder();
		String tmp = null;
		for (int i = 0; i < b.length; i++) {
			tmp = (Integer.toHexString(b[i] & 0xFF));
			if (tmp.length() == 1) {
				sbDes.append("0");
			}
			sbDes.append(tmp);
		}
		return sbDes.toString();
	}

	/**
    *
    * 功能描述：注册加密方法
    *
    * 输入参数：<按照参数定义顺序>
    *
    * @param 参数说明 返回值: 类型 <说明>
    * @return 返回值
    * @throws Exception
    * @throw 异常描述
    * @see 需要参见的其它内容
    */
   public static String encryptRSA(String plainText, String publicKey) throws Exception {
       RSACoder rsa = new RSACoder();
       String utf8String = new String(plainText.getBytes(AES_ENCODING), AES_ENCODING);
       byte[] clientPublicStr = rsa.encryptByPublicKey(utf8String.getBytes(AES_ENCODING), publicKey);
       return Base64Coder.encryptBase64String(clientPublicStr);
   }


   /**
    *
    * 功能描述： RSA Decryption 用私钥解密
    *
    * 输入参数：<按照参数定义顺序>
    *
    * @param 参数说明 返回值: 类型 <说明>
    * @return 返回值
    * @throw 异常描述
    * @see 需要参见的其它内容
    */
   public static String decryptRSA(String cipherText, String privateKey) throws Exception {
       RSACoder rsa = new RSACoder();
       byte[] clientPrivateStr = rsa.decryptByPrivateKey(Base64Coder.decryptBase64(cipherText), privateKey);
       return new String(clientPrivateStr, AES_ENCODING);
   }
	
	/**
	 * 微信公众帐号登录密码加密
	 * 
	 * @param pwd
	 *            公众帐号登录密码
	 * @return 加密后的密码
	 
	public static final String encryptLoginPwd(String pwd) {
		ScriptEngineManager sem = new ScriptEngineManager();
		ScriptEngine se = sem.getEngineByName("js");
		InputStream resourceAsStream = EncryptUtil.class.getClassLoader()
				.getResourceAsStream("conf/file/encrypt.js");
		BufferedReader i = new BufferedReader(new InputStreamReader(
				resourceAsStream));
		String script = "";
		String result = "";
		try {
			while ((script = i.readLine()) != null) {
				result += script;
			}
			se.eval(result);
			Invocable inv2 = (Invocable) se;
			String res = (String) inv2.invokeFunction("m", pwd);
			return res;
		} catch (Exception e) {
			logger.error("EncryptUtil.encryptLoginPwd find wrong!", e);
		}
		return pwd;
	}*/
}
